package com.system.filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年04月20日21:48
 * @Description: <描述>
 */
public class UserLoginRolesAuthorizationFilter extends AuthorizationFilter {
    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        Subject subject = getSubject(servletRequest, servletResponse);

        String[] rolesArray = (String[])o;

        if(rolesArray == null || rolesArray.length == 0) {
            return true;
        }
        //判断角色，只要有任何一个角色就可以访问
        for(int i=0; i<rolesArray.length; i++) {
            boolean b = subject.hasRole(rolesArray[i]);
            if(b) return true;
        }
        return false;
    }
}
